//wire.h
#ifndef __WIRE_H
#define __WIRE_H

#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include <stdint.h>

#define WIRE_BUFSIZ 32

#define WIRE_SPEED_DEFAULT 100000

typedef struct {
    I2C_TypeDef* i2c;
    uint8_t tx_buf[WIRE_BUFSIZ];
    uint8_t rx_buf[WIRE_BUFSIZ];
    uint8_t tx_idx;
    uint8_t rx_idx;
} Wire;
// 初始化
void Wire_begin(Wire* wire, I2C_TypeDef* I2Cx, uint32_t speed);

// 主发送
void Wire_beginTransmission(Wire* wire, uint8_t address);
void Wire_write(Wire* wire, uint8_t data);
int Wire_endTransmission(Wire* wire, uint8_t sendStop); //写寄存器完成时，sendStop设为1；若要读寄存器，sendStop设为0，，并在之后紧接着调用Wire_requestFrom

// 主接收
uint8_t Wire_requestFrom(Wire* wire, uint8_t address, uint8_t quantity); 
uint8_t Wire_read(Wire* wire);

// 快捷函数
int Wire_writeReg(Wire* wire, uint8_t dev_addr, uint8_t reg_addr, uint8_t data);
int Wire_readReg(Wire* wire, uint8_t dev_addr, uint8_t reg_addr, uint8_t* data);
int Wire_readRegs(Wire* wire, uint8_t dev_addr, uint8_t reg_addr, uint8_t* buf, uint8_t len);

// 总线强制恢复（关键！）
void Wire_force_reset(Wire* wire);

#endif
